package com.ydsungan.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class DateUtils {
    public static int daysBetween(Date smdate, Date bdate) throws ParseException
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
        smdate=sdf.parse(sdf.format(smdate));
        bdate=sdf.parse(sdf.format(bdate));
        Calendar cal = Calendar.getInstance();
        cal.setTime(smdate);
        long time1 = cal.getTimeInMillis();
        cal.setTime(bdate);
        long time2 = cal.getTimeInMillis();
        long between_days=(time2-time1)/(1000*3600*24);
        return Integer.parseInt(String.valueOf(between_days));
    }

    public static List<Date> getDateStartAndEnd(int difference)throws ParseException{
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date day = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * difference);
        String dayString = df.format(day);
        String dayStart = dayString + " 00:00:00";
        String dayEnd = dayString + " 23:59:59";

        Date dateStart = null;
        Date dateEnd = null;
        try {
            dateStart = df2.parse(dayStart);
            dateEnd = df2.parse(dayEnd);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<Date> dateList = new ArrayList<>();
        dateList.add(dateStart);
        dateList.add(dateEnd);
        return dateList;
    }

}
